//
//  JXVisitorView.swift
//  swift高仿微博
//
//  Created by peijiaxing on 16/2/25.
//  Copyright © 2016年 peijiaxing. All rights reserved.
//

import UIKit
import SnapKit

let JXVisitorViewMargin: CGFloat = 10


// 创建代理
protocol JXVisitorDelegate:NSObjectProtocol{
    
    //登陆方法
    func didLoginMethod()
    //注册方法
    func didRegisterMethod()

}

class JXVisitorView: UIView {
    
    
    weak var delegate: JXVisitorDelegate?
    
    // 重写构造函数
   override init(frame: CGRect) {
        super.init(frame: frame)
    backgroundColor = UIColor(white: 237/255.0, alpha: 1)
    
    setupUi()
    
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    // 创建一个私有方法
    private func setupUi () {
        
        addSubview(iconImageView)
        addSubview(maskImageView)
        addSubview(homeIconImageView)
        addSubview(messageLabel)
        addSubview(loginbutton)
        addSubview(registerButton)
        
        // 添加约束(运用第三方框架)
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        homeIconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        // 遮罩
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(iconImageView.snp_bottom).offset(JXVisitorViewMargin)
            make.width.equalTo(216)
            
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(JXVisitorViewMargin)
            make.width.equalTo(100)
            make.height.equalTo(30)
            
        }
        loginbutton.snp_makeConstraints { (make) -> Void in
           make.right.equalTo(messageLabel.snp_right)
           make.top.equalTo(messageLabel.snp_bottom).offset(JXVisitorViewMargin)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
    }
    
    // 懒加载
    
    // 首页动画
    private lazy var homeIconImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return img
        
        }()
    
    // 通用图片
    private lazy var iconImageView: UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return img
        }()

    // 转动图片
    func startAnimation () {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.duration = 0.1
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        // 让动画切换界面不被释放
        animation.removedOnCompletion = false
        
        iconImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    
    // 展示文字
    
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.textAlignment = .Center
        label.text = "关注一些人，回这里看看有什么惊喜"
        
        return label
    }()
    
    // 修改显示文字和图片
    func setupInfo(image: String?,message: String,animation:Bool){
       
        if let img = image {
        
        homeIconImageView.image = UIImage(named: img)
        iconImageView.hidden = true
        }else{
            iconImageView.hidden = false
        }
        messageLabel.text = message
    }
    
    // 创建按钮
    
    lazy var loginbutton: UIButton = {
        
        let button = UIButton(type: .Custom)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "loginMethod", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()
    
    lazy var registerButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.addTarget(self, action: "registerMethod", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    // 遮罩
    lazy var maskImageView :UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return img
        
    }()
    
    // 登录方法
    func loginMethod () {
        delegate?.didLoginMethod()
        
    }
    
   
    // 注册方法
    func registerMethod(){
        
        delegate?.didRegisterMethod()
    }

}
